<template>
  <div>
    <h1>上传pdm文件:</h1>
    <el-upload
      action="https://jsonplaceholder.typicode.com/posts/"
      :file-list="fileList"

      :http-request="getFile"
      style="margin-bottom: 20px;"

    >
      <el-button size="small" type="primary">选择文件</el-button>
    </el-upload >
    <el-button size="small" type="success" @click="upload"  style="margin-bottom: 20px;">确认上传</el-button>
    <h1>选择表:</h1>
    <el-transfer
      filterable
      :filter-method="filterMethod"
      filter-placeholder="请输入表中文或英文"
      :titles="['未选择的表', '已选择的表']"
      v-model="selectTable"
      :data="allTable"
      style="margin-bottom: 20px;"
    >
    </el-transfer>
    <el-button size="small" type="success" @click="makeCode">生成代码</el-button>
    <el-button @click="returnList">返回</el-button>
  </div>
</template>

<script>
import request from "../utils/request";
export default {
  name: "run",

  data(){
    return {
      file: {},
      fileList: [],
      selectTable:[],
      allTable:[],
      id:''
    }
  },
  methods: {
    filterMethod(query, item){
      return item.key.indexOf(query) > -1 || item.label.indexOf(query) > -1;
    },
    getFile(item) {
      console.log(item.file)
      this.file = item.file
    },
    upload() {
      const fd = new FormData()
      fd.append('filename', this.file)
      // const config = { headers: { 'Content-Type': 'multipart/form-data' }}
      // this.$request.post('/uploading', fd, config
      // ).then(response => {        this.$message.success(response.retCode)
      // })
      const  that = this;
      request({
        method: "POST",
        url: "/api/Project/uploadPdmFile",
        headers: { 'Content-Type': 'multipart/form-data' },
        data: fd
      }).then(response => {
        if (response.data.status == "1") {
          debugger;
           that.allTable = response.data.list;
          // response.data.list.forEach(function(c, index) {
          //   that.allTable.push({
          //     key: c.key,
          //     label: c.label
          //   });
          // });
        }
        this.$message(response.data.message);
      })
    },
    //生成代码
    makeCode(){
        var tableCode=[];
        //循环选择表放到数组
        if(this.selectTable.length==0){
          this.$message("请选择表!");
          return;
        }
        debugger;
        for(var i=0;i<this.selectTable.length;i++){
          tableCode.push(this.selectTable[i]);
        }
        this.$confirm("是否生成代码?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          request({
            method: "POST",
            url: "/api/Project/run?projectId="+this.id+"&tableCode="+tableCode
          })
            .then(res => {
              if (res.data.status == "1") {

                this.$message(res.data.message);
              } else {
                this.$message(res.data.message);
                return false;
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(err => {
            console.log(err);
          });
    },
    returnList(){
      this.$router.push({path: "/project/list"});
    }
  },
  mounted() {
    console.log("enter mounted!");
    this.id = this.$route.params.id;
  }

}
</script>

<style scoped>

</style>
